package com.my.control
{
	import com.my.control.validate.AlterPassValidate;
	import com.my.service.RemoteServiceFactory;
	import com.my.view.popup.AlterPassView;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.rpc.events.ResultEvent;
	
	public class AlterPassControl extends AbstractControl implements IControl
	{
		private static var instance : AlterPassControl = null;
		
		private var alterPassValidate : AlterPassValidate;
		
		public function AlterPassControl(value:UIComponent)
		{
			super(value);
			alterPassValidate = new AlterPassValidate(value);
		}
		
		public static function getInstance():AlterPassControl{
			return instance;
		}
		
		public static function startup(value : UIComponent):void{
			instance = new AlterPassControl(value);
		}
		
		public override function addEventListener():void{
			getView().alterPassBt.addEventListener(MouseEvent.CLICK,alterPassBtClickHandler);
		} 
		
		public function alterPassBtClickHandler(event:MouseEvent):void{
			if(alterPassValidate.validate()){
				var username:String = getView().username.text;
				var oldPass:String = getView().oldPass.text;
				var newPass:String = getView().newPass.text;
				var code:String = getView().code.text;
					
				RemoteServiceFactory.getInstance().userInfoService.addEventListener(ResultEvent.RESULT,function(e:ResultEvent):void{
					RemoteServiceFactory.getInstance().userInfoService.removeEventListener(ResultEvent.RESULT,arguments.callee);
					
					var v : int = e.result as int;
					switch(v){
						case -1:
							getView().alterResult.text = "此用户名不存在.";
							getView().alterPassPic.loadCode();
							break;
						case -2:
							getView().alterResult.text = "旧密码错误.";
							getView().alterPassPic.loadCode();
							break;
						case -3:
							getView().alterResult.text = "验证码错误.";
							getView().alterPassPic.loadCode();
							break;
						case 1:
							Alert.show("修改成功.");
							getView().closeWin();
							break;
					}
				});
				RemoteServiceFactory.getInstance().userInfoService.alterPass(username,oldPass,newPass,code);
			}
		}
		
		
		public function getView():AlterPassView{
			return AlterPassView(view);
		}
	}
}